/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package it.magostini.mailservice.sender;

import java.util.HashMap;
import java.util.Map;
import javax.mail.internet.MimeMessage;
import org.apache.log4j.Logger;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.ui.velocity.VelocityEngineUtils;

/**
 *
 * @author marco
 * 
 */
public class PreRegistationSender implements Runnable{
     Logger log =Logger.getLogger("INTEGRATION-MAILSERVICE");
   


    private JavaMailSender mailSender;
    private String email;
    private String username;
    private String password;
    private VelocityEngine velocityEngine;
    private String url;

    public PreRegistationSender(JavaMailSender mailSender, String email, String username, String password, VelocityEngine velocityEngine, String url) {
        this.mailSender = mailSender;
        this.email = email;
        this.username = username;
        this.password = password;
        this.velocityEngine = velocityEngine;
        this.url = url;
    }

    @Override
    public void run() {

        
        MimeMessagePreparator preparator = new MimeMessagePreparator() {

                    @Override
                    public void prepare(MimeMessage mimeMessage) throws Exception {
                        
                        MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true);
                        message.setTo(email);
                        message.setSubject("Registrazione WaveWish");
                        message.setFrom("WaveWish@wavewish.it"); // could be parameterized...
                        
                        Map model = new HashMap();
                        
                        model.put("username", username);
//                        model.put("password", password);
                        model.put("email", email);
                        
                        model.put("url", url);
                        String text = VelocityEngineUtils.mergeTemplateIntoString(
                                velocityEngine, "template/registration-confirmation.vm", model);
                        log.info("set Text ");
                        message.setText(text, true);

//            FileSystemResource res = new FileSystemResource("/home/marco/Scrivania/banner_hdr_address.jpg");
//            InputStreamResource res
//                    =
//                    new InputStreamResource(this.getClass().
//                    getResourceAsStream("/image/banner_hdr_address.jpg"));
//                    res.getInputStream().close();
//                        log.info("add image ");
//                        message.addInline("imageHeader", new ClassPathResource("/image/Header-mail.jpg"));


                    }
                };
                

                try{
                mailSender.send(preparator);
                }catch(Exception e){
                    log.error("NON SENDING EMAIL FOR REGISTRATION TO "+email,e);
                    return;
                }
                log.info("EMAIL SEND FOR REGISTRATION TO "+email);
    }

}
